package com.zhuss.order.processor;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.order.order.CreatePayOrderRefundBo;
import com.zhuss.bean.bo.order.order.CreatePrefundBo;
import com.zhuss.bean.bo.order.order.WxRefundBo;
import com.zhuss.bean.vo.order.order.CreatePayOrderRefundVo;
import com.zhuss.bean.vo.order.order.DetailOrderItemVo;
import com.zhuss.bean.vo.order.order.DetailOrderVo;
import com.zhuss.bean.vo.order.order.WxRefundVo;
import com.zhuss.bean.vo.product.train.DetailTrainVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.OrderStatus;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.order.db.dao.ext.OrderDao;
import com.zhuss.order.db.dao.ext.OrderItemDao;
import com.zhuss.order.db.dao.ext.OrderRefundDao;
import com.zhuss.order.db.dao.ext.PrefundDao;
import com.zhuss.order.db.dao.ext.PrepayDao;
import com.zhuss.order.db.entity.OrderRefund;
import com.zhuss.order.db.entity.Prefund;
import com.zhuss.product.processor.cache.TrainCache;
import com.zhuss.util.JsonUtil;
import com.zhuss.util.NoUtil;

/**
 * 创建退款单 processor
 * @Author: zhushanshuo
 * @Date: 2020/11/10 20:18
 */
@Component("orderRefundCreate")
public class OrderRefundCreate extends AbstractProcessor<CreatePayOrderRefundVo, CreatePayOrderRefundBo> implements Processor<CreatePayOrderRefundVo, CreatePayOrderRefundBo> {

    @Autowired
    private OrderRefundDao orderRefundDao;
    
    @Autowired
    private OrderDao orderDao;
    
    @Autowired
    private OrderItemDao orderItemDao;
    
    @Autowired
    private OrderRead orderRead;
    
    @Autowired
    @Qualifier("trainCache")
    private TrainCache trainCache;
    
    @Autowired
    @Qualifier("prefundCreate")
    private Processor<Object, CreatePrefundBo> prefundCreate;
    
    @Autowired
    private PrefundDao prefundDao;
    
    @Autowired
    private PrepayDao prepayDao;
    
    @Autowired
    @Qualifier("wxRefundProcessor")
    private Processor<WxRefundVo, WxRefundBo> wxRefundProcessor;
    
    /**
     * 校验
     */
    @Override
    public void check(CreatePayOrderRefundBo param) throws DzcxException {
    	Long orderId = param.getOrderId();
    	if (orderId == null || orderId <= 0) {
    		throw new DzcxException(ExceptionCode.订单ID不存在);
    	}
    	
    	DetailOrderVo order = orderRead.detail(orderId);
    	DetailTrainVo train = trainCache.get(param.getTrainId());
    	
    	
    	
    	if (order == null || train == null) {
    		throw new DzcxException(ExceptionCode.订单ID不存在);
    	}
    	
    	DetailOrderItemVo orderItem = orderItemDao.getTrainByTake(orderId, train.getTrainId(), OrderStatus.支付成功.key());
    	if (orderItem == null) {
    		throw new DzcxException(ExceptionCode.无需退款);
    	}
    	Integer status = order.getStatus();
    	Integer ticketStatus = orderItem.getStatus();
    	if (OrderStatus.已确认上车.sameKey(ticketStatus) || OrderStatus.已确认上车.sameKey(status)) {
    		throw new DzcxException(ExceptionCode.已确认上车_不能退款);
    	}
    	
    	param.setOrder(order);
    	param.setTrain(train);
    	param.setOrderItem(orderItem);
    	
    }
    
    /**
     * 操作方法
     * @throws DzcxException 
     */
    @Override
    public CreatePayOrderRefundVo process(CreatePayOrderRefundBo param) throws DzcxException {
    	Long orderId = param.getOrderId();
    	
    	DetailOrderVo order = param.getOrder();
    	DetailTrainVo train = param.getTrain();
    	DetailOrderItemVo orderItem = param.getOrderItem();
    	
    	OrderRefund record = new OrderRefund();
    	record.setOrderId(orderId);
    	record.setOrderNo(order.getOrderNo());
    	record.setOrderRefundNo(NoUtil.REFUND + order.getOrderNo() + "|" + param.getTrainId());
    	record.setTrainId(param.getTrainId());
    	record.setDate(train.getDate());
    	record.setTransactionId(order.getTransactionId());
    	record.setStatus(OrderStatus.已提交退款申请.key());
    	record.setPrice(orderItem.getPrice());
    	record.setRefundTotalPrice(orderItem.getPrice());
    	record.setRefundPercent(100l);
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	
    	String orderRefundNo = record.getOrderRefundNo();
    	if (orderRefundDao.getByOrderRefundNo(orderRefundNo) != null) {
    		throw new DzcxException(ExceptionCode.请勿重复退款);
    	}
    	
    	orderRefundDao.create(record);
    	param.setTargetId(record.getOrderRefundId());
    	
    	
    	CreatePrefundBo prefundParam = new CreatePrefundBo();
    	BeanUtils.copyProperties(param, prefundParam);
    	prefundParam.setOrderRefundId(param.getTargetId());
    	prefundParam.setOrderId(record.getOrderId());
    	prefundParam.setApplyRefundFee(record.getPrice());
    	prefundParam.setApplyTotalFee(record.getPrice());
    	prefundParam.setRefundFee(record.getPrice());
    	prefundCreate.process(prefundParam);
    	
    	WxRefundBo wxPayParam = new WxRefundBo();
    	BeanUtils.copyProperties(param, wxPayParam);
    	wxPayParam.setNonceStr(NoUtil.NONCE + NoUtil.genNonceStr());
    	wxPayParam.setOrderNo(record.getOrderNo());
    	wxPayParam.setTotalFee(order.getTotalPrice());
    	wxPayParam.setApplyRefundFee(record.getRefundTotalPrice());
    	wxPayParam.setOrderRefundNo(record.getOrderRefundNo());
    	WxRefundVo vo = wxRefundProcessor.process(wxPayParam);
    	String wxRefundId = vo.getWxRefundId();
    	
    	Prefund prefund = new Prefund();
    	BeanUtils.copyProperties(vo, prefund);
    	prefund.setPrefundId(prefundParam.getTargetId());
    	prefundDao.updateByPrimaryKeySelective(prefund);
    	
    	prefundDao.refresh(record.getOrderRefundId(), wxRefundId);
    	
    	Date now = new Date();
    	
    	orderItemDao.refreshItemStatus(record.getOrderId(), param.getTrainId(), OrderStatus.已提交退款申请.key(), param.getConcurrentUserName(), param.getConcurrentUserId(), now);
        
        return new CreatePayOrderRefundVo(param.getTargetId(), record.getOrderRefundNo());
    }
    
    /**
     * 操作日志
     */
	@Override
	public CreatePayOrderRefundVo log(CreatePayOrderRefundBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.订单.key());
    	record.setAction(LogActionEnum.退款单创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getOrderId() + "");
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
